package jmine.tec.persist.chinese;

import org.hibernate.SessionFactory;
import org.hibernate.engine.SessionFactoryImplementor;
import org.springframework.beans.factory.FactoryBean;

import bancosys.tec.persist.authorization.AuthorizationContext;
import bancosys.tec.persist.hibernate.PartitionSessionFactory;

/**
 * FactoryBean que tenta buscar o SessionFactoryImplementor
 * 
 * @author lundberg
 */
public class SessionFactoryImplementorFinder implements FactoryBean {

    private SessionFactory sessionFactory;

    /**
     * {@inheritDoc}
     */
    public Object getObject() throws Exception {
        if (this.sessionFactory instanceof PartitionSessionFactory) {
            return ((PartitionSessionFactory) this.sessionFactory).getPureSessionFactory(AuthorizationContext.hot);
        } else {
            return this.sessionFactory;
        }
    }

    /**
     * {@inheritDoc}
     */
    public Class getObjectType() {
        return SessionFactoryImplementor.class;
    }

    /**
     * {@inheritDoc}
     */
    public boolean isSingleton() {
        return true;
    }

    /**
     * @param sessionFactory the sessionFactory to set
     */
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}
